<script 
	src="http://maps.google.com/jsapi?ie=utf-8&amp;oe=utf-8&amp;key=ABQIAAAAnfs7bKE82qgb3Zc2YyS-oBT2yXp_ZAY8_ufC3CFXhHIE1NvwkxSySz_REpPq-4WZA27OwgbtyR3VcA"
  type="text/javascript">
</script>

<script type="text/javascript">

	google.load("maps", "2.x", {"language" : "en"});
	var map;
	var latlng;
	var radio = new Array();
	var index = 0;

	function initialize()
	{
	  if (GBrowserIsCompatible())
	  {
	    map = new GMap2(document.getElementById("map"));
	    
	    map.addMapType(G_PHYSICAL_MAP);
	    
	    <?php if( count($image) > 0) { 
	    	for($k = 0; $k<count($image); $k+=1) {?>
					var boundary<?php echo $k ?> = new GLatLngBounds(new GLatLng(<?php echo $image[$k]->getLower()->lat ?>, <?php echo $image[$k]->getLower()->lng ?>), new GLatLng(<?php echo $image[$k]->getUpper()->lat ?>, <?php echo $image[$k]->getUpper()->lng ?>));
					var img<?php echo $k ?> = new GGroundOverlay("<?php echo $image[$k]->getImagePath() ?>", boundary<?php echo $k ?>);
					map.addOverlay(img<?php echo $k ?>);
					<?php 
				}
	    } ?>
	    
	    map.addControl(new google.maps.LargeMapControl());
	    map.addControl(new GScaleControl());
	    map.addControl(new google.maps.MapTypeControl());
	    
	    map.enableScrollWheelZoom();
	    
	    map.setCenter(new google.maps.LatLng(<?php echo $centerPos->lat ?>, <?php echo $centerPos->lng ?>), 14, G_NORMAL_MAP);
	    
		var ActiveMarker; // GMarker that changes position as the user clicks around the map.
	    
	    GEvent.addListener(map, "click", function(overlay, point)
        { 
        	var isMarker = document.getElementById('isAntenna').value; // 1 if the user has clicked at least once to place a marker.
        	if (isMarker == "false")
            {
          		// Generate a new GIcon, allowing the marker to use custom images.
          		var MyIcon = new GIcon();                        // create a new Google Icon object
				MyIcon.image = "/omwpt/media/icon/Antenna.png";  // image file name
				MyIcon.iconSize = new GSize(40, 40);             // width (px) and height (px)
				MyIcon.iconAnchor = new GPoint(20, 20);          // base of the icon
				MyIcon.iconWindowAnchor = new GPoint(0, 0);      // info window base 
				
				var newMarker = new GMarker(point, {draggable: true});
				newMarker.index = index;
				newMarker.height = document.getElementById('height').value;
				newMarker.erp = document.getElementById('erp').value;
				newMarker.frequency = document.getElementById('frequency').value;
				updateIndex(newMarker);
	            ActiveMarker = newMarker;
	            
//	          GEvent.addListener(ActiveMarker, "click", function()
//		        {
//		    		setLatLng(ActiveMarker.getLatLng());
//		        });
		        
//		        GEvent.addListener(ActiveMarker, "drag", function() 
//		        {
//					setLatLng(ActiveMarker.getLatLng());
//				});
				
				GEvent.addListener(newMarker, "click", function()
				{
					ActiveMarker = newMarker;
					updateIndex(newMarker);
				});
				
				GEvent.addListener(newMarker, "drag", function()
				{
					updateIndex(newMarker);
					ActiveMarker = newMarker;
				});
		     
	            document.getElementById('isAntenna').value = "true";
	            
	            newRadio = new Object();
	            newRadio.lat = point.lat;
	            var tempIndex = index * 6;
	            radio[tempIndex] = newMarker.index;
	            radio[tempIndex + 1] = point.lat();
	            radio[tempIndex + 2] = point.lng() * -1;
	            radio[tempIndex + 3] = document.getElementById('height').value;
	            radio[tempIndex + 4] = document.getElementById('erp').value;
	            radio[tempIndex + 5] = document.getElementById('frequency').value;
	            index = index + 1;
	            
	            document.getElementById('radioArray').value = radio.toString();
	            
				map.addOverlay(ActiveMarker);
          	}
          	else
          	{
          		ActiveMarker.setLatLng(point);
          		updateIndex(ActiveMarker);
          	}
          	setLatLng(point);
          });
	  
	  }
	}
	 
	function setLatLng(point)
	{
		var newLat = new Number(point.lat());
		var newLng = new Number(point.lng());
		newLng = newLng * -1;
		document.getElementById('lat').value = newLat.toPrecision(7);
		document.getElementById('lng').value = newLng.toPrecision(7);
	} 
	
	function updateIndex(newMarker)
	{
		document.getElementById('antennaIndex').value = newMarker.index;
		document.getElementById('lat').value = newMarker.getLatLng().lat();
		document.getElementById('lng').value = newMarker.getLatLng().lng() * -1;
		document.getElementById('height').value = newMarker.height;
		document.getElementById('erp').value = newMarker.erp;
		document.getElementById('frequency').value = newMarker.frequency;
		radio[newMarker.index * 6 + 1] = newMarker.getLatLng().lat();
		radio[newMarker.index * 6 + 2] = newMarker.getLatLng().lng() * -1;
		radio[newMarker.index * 6 + 3] = newMarker.height;
		radio[newMarker.index * 6 + 4] = newMarker.erp;
		radio[newMarker.index * 6 + 5] = newMarker.frequency;
		document.getElementById('radioArray').value = radio.toString();
	}
	
	function updateDirText(value)
	{
  		document.getElementById('dirValue').value = value;
  	}
      
  	function updateDirSlider(value)
  	{
  		document.getElementById('dirSlider').value = value;
  	}
</script>